/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package studente;

import ejb.GestioneFileLocal;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import jpa.Documento;

/**
 *
 * @author Administrator
 */
@ManagedBean(name="ListaFile")
@SessionScoped
public class ListaFile {

    public static final int MAXROW = 30;

    @EJB
    GestioneFileLocal gf;
    private List<Documento> file;
    private int page;
    private int numTuple = -1;
    private int numPage;
    private boolean successivo;
    private boolean precedente;
    private String message;
    private int start;
    private int end;

    /** Creates a new instance of HomeBean */
    public ListaFile() {
        precedente = true;
        successivo = false;
        page = 0;
        numTuple = -1;
        numPage = 1;
        
    }



    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public boolean isPrecedente() {
        return precedente;
    }

    public boolean isSuccessivo() {
        return successivo;
    }

    public int getPage(){
        return page;
    }

    public Collection<Documento> getFile(){
        update();
        file = gf.listaFile(start,end);
        return Collections.unmodifiableCollection(file);
    }

    private void update(){
        numTuple = gf.numeroFile();
        if(numTuple == 0){
            numPage = 1;
            return;
        }
        if(numTuple <= MAXROW){
            successivo = true;
        }

        numPage = numTuple / MAXROW;
        if(numPage > 1){
            successivo = false;
        }
        if(numTuple%MAXROW != 0) {
            numPage +=1;
        }
        start = page * MAXROW;
        end = ((page +1) * MAXROW);

    }

    public String getPagine(){
        return " Pagina " + (page + 1) + " di " + numPage +" ";
    }

    public String precedente(){
        page--;
        if(page <= 0){
            page = 0;
           precedente = true;

        }
        if(page < numPage) {
            successivo = false;
        }
        update();
        return "f";
    }

    public String successivo(){
        page++;
        if(page >= (numPage -1)){
            page = (numPage -1);
            successivo = true;

        }
        if(page > 0) {
            precedente = false;
        }
        update();
        return "f";
    }


    public int getNumPage(){
       return numPage;
    }



}
